package com.bc.bcapi.service;

import com.bc.bcapi.dao.T_User_browseMapper;
import com.bc.bccore.redis.RedisService;
import com.bc.bcmodel.domain.T_User_browseModel;
import com.bc.commonentity.common.JsonResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Name TestService
 * @Description TODO
 * @Author douhaichao
 * @Date 2018/9/11 10:17
 * @Version 1.0
 **/
@Service
public class TestService {

    @Resource
    T_User_browseMapper t_user_browseMapper;
    @Resource
    RedisService redisService;
    static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);

    public JsonResult methodTest() {
        JsonResult js = new JsonResult();
        fixedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
//                    insertIntegralRecord(em, business_id, user_id);
                    insertUserBrowse();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        return js;
    }

    private JsonResult insertUserBrowse() {
        JsonResult js = new JsonResult();
        String key = "key_insertUserBrowse";
        try {
            if (!redisService.setnx(key, "1")) {
                js.setMsg("当前被锁定");
                return js;
            }
            try {
                Thread.sleep(1000 * 60);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            T_User_browseModel t_user_browseModel = new T_User_browseModel();
            t_user_browseModel.setUser_id("1");
            t_user_browseModel.setPid("P20180821000002002");
            t_user_browseMapper.insert(t_user_browseModel);
        } finally {
            redisService.remove(key, true);
        }
        return js;
    }

}
